/**
 * 
 */
package org.vsg.stock.basic.service.volfigure;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.vsg.stock.basic.domain.VolFigureDetails;
import org.vsg.stock.basic.domain.VolFigureKlineDaily;
import org.vsg.stock.basic.domain.VolFigureKlineDailyPK;
import org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.indicator.kline.ZigIndicatorResultItem;

/**
 *  处理缩量柱模式2bb，连续缩量三天,其中第二天为上升阳线,出增量(非倍量)，第三天继续缩量下跌
 *  
 * @author Bill Vison
 *
 */
public class DecreaseMod2bbProcessor implements VolFigureKlineDailyProcessor {

	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

	DecimalFormat nf = new DecimalFormat("##0.000");	
	
	private Set<String> dailyKeys = new HashSet<String>();	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#processDailyData(java.util.List)
	 */
	@Override
	public List<VolFigureKlineDaily> processDailyData(
			List<StockDailyKLine> stkDailyKLineList) {

		List<VolFigureKlineDaily> klineDailyList = new Vector<VolFigureKlineDaily>();		
		
		double rate = 0.95;
		
		StockDailyKLine startLeveKline = null;
		double levelOpen = 0;
		for (int i = 0 ; i < stkDailyKLineList.size() ; i++) {

			boolean preCheckResult = preCheckFit(stkDailyKLineList , i );

			if (!preCheckResult) {
				continue;
			}
			startLeveKline = stkDailyKLineList.get(i);

			//System.out.println("this o : " + startLeveKline.getDate());			
			//System.out.println(startLeveKline.getDate());

/*			int passIndex = handleInvoke(stkDailyKLineList , i);
			
			if ( passIndex > 0) {

				String indexLine = startLeveKline.getId().getStockCode()+","+detail.getIdDetails()+","+df.format( startLeveKline.getDate() );
				
				if (!dailyKeys.contains( indexLine) ) {
					Date markEndDate = stkDailyKLineList.get( i+passIndex ).getDate();
					
					// --- mark the date ---
					VolFigureKlineDailyPK idPk = new VolFigureKlineDailyPK();
					idPk.setIdVolFigureDetails( this.detail.getIdDetails() );
					idPk.setStockCode( startLeveKline.getId().getStockCode() );
					idPk.setStockStartDate( startLeveKline.getId().getDate() );

					// --- gen new data object ---
					VolFigureKlineDaily dailyInst = new VolFigureKlineDaily();
					dailyInst.setId( idPk );
					dailyInst.setStockDateEnd( markEndDate );
					
					klineDailyList.add( dailyInst );
				}
				
				i += passIndex; 
				
			}*/
		}
		

		return klineDailyList;
	}

	// --- check the list is fit to char processor
	private boolean preCheckFit(List<StockDailyKLine> stkDailyKLineList , int index) {
		

		double maxPrice = 0;
		double maxVol = 0;
		for (int i = 0 ; i < 4 ; i++) {
			if ( index + i >= stkDailyKLineList.size() ) {
				return false;
			}
			int currentIndex = index + i;

			// --- no need the first value
			if ( i == 0 ) {
				StockDailyKLine curDailyKLine = stkDailyKLineList.get(currentIndex);				
				String valueStr = nf.format( (curDailyKLine.getOpen() + curDailyKLine.getClose() ) / 2 );
				maxPrice = Double.parseDouble( valueStr );
				maxVol = curDailyKLine.getVol();
				continue;
			}
			
			double compareValue =  Math.abs( (double)stkDailyKLineList.get(currentIndex).getVol() - stkDailyKLineList.get(currentIndex-1).getVol());
			
			double compareRate = compareValue / stkDailyKLineList.get(currentIndex-1).getVol();
			
			// --- 两者相差不能太少 ---
			if ( compareRate < 0.05) {
				return false;
			}
			
			long firstVol = stkDailyKLineList.get(currentIndex).getVol();
			long secondVol = stkDailyKLineList.get(currentIndex-1).getVol();
			
			if ( maxVol < firstVol) {
				return false;
			}
			

			// --- second value not keep value ---
			if ( i == 2 ) {
				if ( stkDailyKLineList.get(currentIndex).getYieldRate() < 0) {
					return false;
				}
				if ( firstVol - secondVol < 0) {
					return false;
				}

			} else {
				if ( firstVol - secondVol > 0) {
					return false;
				}
				
			}
			
		}
		return true;
	}
	
	private int handleInvoke(List<StockDailyKLine> stkDailyKLineList , int index) {
	
		
		int i = 0;
		double maxPrice = 0;
		double maxVol = 0;		
		while (true) {
			int currentIndex = index + i;
			// --- pass current idnex ---
			if ( currentIndex == index) {
				i++;
				StockDailyKLine curDailyKLine = stkDailyKLineList.get(currentIndex);
				String valueStr = nf.format( (curDailyKLine.getOpen() + curDailyKLine.getClose() ) / 2 );
				maxPrice = Double.parseDouble( valueStr );
				maxVol = curDailyKLine.getVol();				
				continue;
			}
			StockDailyKLine preStkDailyKLine = stkDailyKLineList.get( currentIndex - 1 );
			StockDailyKLine lastStkDailyKLine = stkDailyKLineList.get( currentIndex );

			long firstVol =lastStkDailyKLine.getVol();
			long secondVol = preStkDailyKLine.getVol();
			// --- second value not keep value ---
			if ( maxVol < firstVol) {
				break;
			}
			
			// --- second value not keep value ---
			if ( i < 3) {
				if ( firstVol - secondVol > 0) {
					break;
				}				
				// ---- next price not over ----
				if ( stkDailyKLineList.get(currentIndex).getClose() > maxPrice) {
					break;
				}
				if ( stkDailyKLineList.get(currentIndex).getYieldRate() >= 0) {
					break;
				}

			} else {
				if ( firstVol - secondVol < 0) {
					break;
				}				
				// ---- next price not over ----
				if ( stkDailyKLineList.get(currentIndex).getClose() > maxPrice) {
					break;
				}
				if ( stkDailyKLineList.get(currentIndex).getYieldRate() <= 0) {
					break;
				}
				
			}
			i++;
		}

		// --- not include the current date
		return i-1;
	}
	
	
	private List<ZigIndicatorResultItem> crests;

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCrestsInCycle(java.util.List)
	 */
	@Override
	public void setCrestsInCycle(List<ZigIndicatorResultItem> crests) {
		this.crests = crests;

	}

	@Override
	public void setExistedVolFigureKineDaily(
			List<VolFigureKlineDaily> volFigureKlineDailyList) {
		// --- get the daily ---
		VolFigureKlineDailyPK id = null;
		for (VolFigureKlineDaily daily : volFigureKlineDailyList) {
			id = daily.getId();
			String keyLine = id.getStockCode()+ "," + id.getCodeVolFigureDetails()+"," + df.format( id.getStockStartDate() );
			dailyKeys.add( keyLine );
		}
		
	}

	private VolFigureDetails detail;	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCurrentVolFigureDetails(org.vsg.stock.basic.domain.VolFigureDetails)
	 */
	@Override
	public void setCurrentVolFigureDetails(VolFigureDetails detail) {
		this.detail = detail;

	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setTroughsInCycle(java.util.List)
	 */
	@Override
	public void setTroughsInCycle(List<ZigIndicatorResultItem> troughs) {
		// TODO Auto-generated method stub

	}

}
